package sc.export
{
	import com.adobe.share.api.ShareAPI;
	import com.adobe.share.api.ShareAPIEvent;
	import com.adobe.share.api.ShareAPIToken;
	import com.adobe.share.api.ShareAPIUser;
	import sc.*;
	import flash.events.Event;
	import flash.filesystem.*;
	import flash.events.DataEvent;
	import mx.controls.Alert;
	
	public class ShareExport extends BaseExport
	{
		private var api:ShareAPI = null;
		private var user:ShareAPIUser = null;
		
		public function ShareExport(username:String, pw:String)
		{
			super();
			api = new ShareAPI("e2fb505089b37700b5f7a57008980206", "da53130f47f684c11bab297ba10a62c9");
			user = new ShareAPIUser(username, pw);
			Alert.show("We are sorry, but the Adobe Share API is currently unavailable. Please use the save to local " + 
					"filesystem option instead and then upload to Share manually.", 'Share error',Alert.OK);
		}
		
		public function publishAsHtmlDir(exten:String, rootPath:String):void 
		{
			ext = exten;
			path = rootPath;
			
			var token:ShareAPIToken = api.login(user);
			token.addEventListener(ShareAPIEvent.API_AUTHENTICATED, saveDirOfHtmlHandler); 
			token.addEventListener(ShareAPIEvent.API_FAILURE, onFault);
		}
		
		public  function saveDirOfHtmlHandler(event:ShareAPIEvent):void 
		{
			var hell:texy_editor_tab = $.main.tabCont.selectedChild as  texy_editor_tab;
			var lim:uint = hell.output.numChildren;
			api.addFolder(user, "Flexy export", "This is your site as exported from Flexy", null);	
			for(var i:uint = 0; i < lim; i++) {
				var tmp:File = File.createTempFile();
				var resTab:Resultrender = hell.output.getChildAt(i) as Resultrender;
				saveTab(tmp, resTab, false);
				api.uploadFile(user, tmp, null, resTab.label+".html", "Export from Flexy!", true);
				tmp.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, 
					function(event:DataEvent):void
					{
						trace("file uploaded");
					}
				); 
			}
			$.main.ws.selectedIndex = 0;
			currentFile = null;	
		}
		
		public function publishAsSingleHtml(rootPath:String):void 
		{
			//ext = exten;
			path = rootPath;
			var token:ShareAPIToken = api.login(user);
			token.addEventListener(ShareAPIEvent.API_AUTHENTICATED, saveSingleHtmlHandler); 
			token.addEventListener(ShareAPIEvent.API_FAILURE, onFault);
		}
		
		public function saveSingleHtmlHandler(event:ShareAPIEvent):void 
		{
			var hell:texy_editor_tab = $.main.tabCont.selectedChild as  texy_editor_tab;
			var tmp:File = File.createTempFile();
			var resTab:Resultrender = hell.output.selectedChild as Resultrender;
			saveTab(tmp, resTab, false);
			api.uploadFile(user, tmp, null, resTab.label+".html", "Export from Flexy!", true);
			tmp.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, 
				function(event:DataEvent):void
				{
					trace("file uploaded");
				}
			); 
			$.main.ws.selectedIndex = 0;
			currentFile = null;			
		}
		
		public function publishAsZip(rootPath:String):void 
		{
			//ext = exten;
			path = rootPath;
			var token:ShareAPIToken = api.login(user);
			token.addEventListener(ShareAPIEvent.API_AUTHENTICATED, saveZipOfHtmlHandler); 
			token.addEventListener(ShareAPIEvent.API_FAILURE, onFault);
		} 
		
		public function saveZipOfHtmlHandler(event:ShareAPIEvent):void 
		{
			var tmp:File = File.createTempFile();
			saveZipOfHtml(tmp);
			api.uploadFile(user, tmp, null, "export.zip", "Export from Flexy!", true);
			tmp.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, 
				function(event:DataEvent):void
				{
					trace("file uploaded");
				}
			); 
			$.main.ws.selectedIndex = 0;		
		}
		
		private function onFault(event : ShareAPIEvent) : void {
				trace(event.error);
		}
		
	}
	
}